package com.sakura.mall.coupon.seckillPromotion.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.coupon.seckillPromotion.domain.entity.SeckillPromotionEntity;
import com.sakura.mall.coupon.seckillPromotion.domain.request.AddSeckillPromotionRequest;
import com.sakura.mall.coupon.seckillPromotion.domain.request.UpdateSeckillPromotionRequest;
import com.sakura.mall.coupon.seckillPromotion.domain.request.SeckillPromotionPageRequest;
import com.sakura.mall.coupon.seckillPromotion.domain.vo.SeckillPromotionVo;
import com.sakura.mall.coupon.seckillPromotion.mapper.SeckillPromotionMapper;
import com.sakura.mall.coupon.seckillPromotion.service.SeckillPromotionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 秒杀活动 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class SeckillPromotionServiceImpl extends ServiceImpl<SeckillPromotionMapper, SeckillPromotionEntity> implements SeckillPromotionService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSeckillPromotionRequest request) {
        // todo 其他校验逻辑
        SeckillPromotionEntity seckillPromotion = request.convert(SeckillPromotionEntity.class);
        this.save(seckillPromotion);
    }

    @Override
    public Page<SeckillPromotionVo> queryPage(SeckillPromotionPageRequest request) {
        IPage<SeckillPromotionEntity> qp = request.toPage();
        LambdaQueryWrapper<SeckillPromotionEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<SeckillPromotionVo> ret = qp.convert(entity -> entity.convert(SeckillPromotionVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateSeckillPromotionRequest request) {
        SeckillPromotionEntity entity = request.convert(SeckillPromotionEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

